struct Solution;

impl Solution {
    pub fn num_moves_stones_ii(mut stones: Vec<i32>) -> Vec<i32> {
        let n = stones.len();
        let n_i32 = n as i32;
        let mut low = n_i32;

        stones.sort();
        let hi = i32::max(stones[n - 1] - stones[1] - n_i32 + 2, stones[n - 2] - stones[0] - n_i32 + 2);

        let mut i = 0;
        for (index, val) in stones.iter().enumerate() {
            while val - stones[i] >= n_i32 {
                i += 1;
            }
            if index - i == n - 2 && val - stones[i] == n_i32 - 2 {
                low = i32::min(low, 2);
            } else {
                low = i32::min(low, (n - (index - i + 1)) as i32);
            }
        }

        vec![low, hi]
    }
}


#[cfg(test)]
mod test {
    use crate::Solution;

    #[test]
    fn test_01() {
        let stones = vec![1, 2, 5];

        assert_eq!(Solution::num_moves_stones_ii(stones), vec![2, 2]);
    }
}

fn main() {
    let stones = vec![1, 2, 5];

    assert_eq!(Solution::num_moves_stones_ii(stones), vec![2, 2]);
}
